home *** CD-ROM | disk | FTP | other *** search
/ Network PC / Network PC.iso / amiga utilities / communication / internet / amitcp3.0b / src.lha / src / amitcp / net / if.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-08  |  18.8 KB  |  772 lines

  1. RCS_ID_C="$Id: if.c,v 3.1 1994/02/03 03:50:38 ppessi Exp $";
  2. /*
  3.  * Copyright (c) 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>,
  4.  *                    Helsinki University of Technology, Finland.
  5.  *                    All rights reserved.
  6.  *
  7.  * if.c --- Generic Network Interface Routines
  8.  *
  9.  * Last modified: Thu Feb  3 01:42:51 1994 ppessi
  10.  *
  11.  * HISTORY
  12.  * $Log: if.c,v $
  13.  * Revision 3.1  1994/02/03  03:50:38  ppessi
  14.  * Initially tested version
  15.  *
  16.  * Revision 1.22  1994/01/13  07:36:55  jraja
  17.  * Added findid() function to support gethostid() system call.
  18.  *
  19.  * Revision 1.21  1993/10/29  02:00:17  ppessi
  20.  * Added SIOCGARPT ioctl.
  21.  *
  22.  * Revision 1.20  1993/09/19  21:14:18  jraja
  23.  * Fixed a bug with '/' placement in ifconf().
  24.  *
  25.  * Revision 1.19  1993/09/09  23:45:36  ppessi
  26.  * Changed the interface name format returned by SIOCGIFCONF.
  27.  */
  28.  
  29. /*
  30.  * Copyright (c) 1980, 1986 Regents of the University of California.
  31.  * All rights reserved.
  32.  *
  33.  * Redistribution and use in source and binary forms, with or without
  34.  * modification, are permitted provided that the following conditions
  35.  * are met:
  36.  * 1. Redistributions of source code must retain the above copyright
  37.  *    notice, this list of conditions and the following disclaimer.
  38.  * 2. Redistributions in binary form must reproduce the above copyright
  39.  *    notice, this list of conditions and the following disclaimer in the
  40.  *    documentation and/or other materials provided with the distribution.
  41.  * 3. All advertising materials mentioning features or use of this software
  42.  *    must display the following acknowledgement:
  43.  *    This product includes software developed by the University of
  44.  *    California, Berkeley and its contributors.
  45.  * 4. Neither the name of the University nor the names of its contributors
  46.  *    may be used to endorse or promote products derived from this software
  47.  *    without specific prior written permission.
  48.  *
  49.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  50.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  51.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  52.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  53.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  54.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  55.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  56.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  57.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  58.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  59.  * SUCH DAMAGE.
  60.  *
  61.  *    @(#)if.c    7.14 (Berkeley) 4/20/91
  62.  */
  63.  
  64. #include <conf.h>
  65.  
  66. #include <sys/param.h>
  67. #include <sys/malloc.h>
  68. #include <sys/mbuf.h>
  69. #include <sys/systm.h>
  70. #include <sys/socket.h>
  71. #include <sys/socketvar.h>
  72. #include <sys/protosw.h>
  73. #include <sys/kernel.h>
  74. #include <sys/ioctl.h>
  75. #include <sys/synch.h>
  76.  
  77. #include <net/if.h>
  78. #ifndef AMITCP
  79. #include <net/if_dl.h>
  80. #endif
  81. #include <netinet/in.h> /* for findid */
  82.  
  83. void ifinit(void);
  84. void if_attach(struct ifnet *);
  85. struct ifaddr *ifa_ifwithaddr(register struct sockaddr *);
  86. struct ifaddr *ifa_ifwithdstaddr(register struct sockaddr *);
  87. struct ifaddr *ifa_ifwithnet(struct sockaddr *);
  88. struct ifaddr *ifa_ifwithaf(register int );
  89. struct ifaddr *ifaof_ifpforaddr(struct sockaddr *, register struct ifnet *);
  90. void link_rtrequest(int, struct rtentry *, struct sockaddr *);
  91. void if_down(register struct ifnet *);
  92. void if_qflush(register struct ifqueue *);
  93. void if_slowtimo(void);
  94. struct ifnet *ifunit(register char *);
  95. int ifioctl(struct socket *, int, caddr_t);
  96. int ifconf(int, caddr_t);
  97.  
  98. /* Compatibility with AmiTCP/IP 2 */
  99. #include <sys/a_ioctl.h>
  100. #include <net/a_if.h>
  101. struct ifnet *aifunit(register char *name);
  102. int aifconf(int cmd, caddr_t data);
  103.  
  104. #include <kern/uipc_domain_protos.h>
  105.  
  106. static char *sprint_d(u_int n, char *buf, int buflen);
  107. int    ifqmaxlen = IFQ_MAXLEN;
  108.  
  109. struct    ifnet *ifnet = NULL;
  110.  
  111. /*
  112.  * Network interface utility routines.
  113.  *
  114.  * Routines with ifa_ifwith* names take sockaddr *'s as
  115.  * parameters.
  116.  */
  117.  
  118. void ifinit(void)
  119. {
  120.     register struct ifnet *ifp;
  121.  
  122.     for (ifp = ifnet; ifp; ifp = ifp->if_next)
  123.         if (ifp->if_snd.ifq_maxlen == 0)
  124.             ifp->if_snd.ifq_maxlen = ifqmaxlen;
  125.     if_slowtimo();
  126. }
  127.  
  128. #ifdef vax
  129. /*
  130.  * Call each interface on a Unibus reset.
  131.  */
  132. ifubareset(uban)
  133.     int uban;
  134. {
  135.     register struct ifnet *ifp;
  136.  
  137.     for (ifp = ifnet; ifp; ifp = ifp->if_next)
  138.         if (ifp->if_reset)
  139.             (*ifp->if_reset)(ifp->if_unit, uban);
  140. }
  141. #endif
  142.  
  143. int if_index = 0;
  144. #ifndef AMITCP
  145. struct ifaddr **ifnet_addrs;
  146. static char *sprint_d();
  147. #endif
  148.  
  149. /*
  150.  * Attach an interface to the
  151.  * list of "active" interfaces.
  152.  */
  153. void
  154. if_attach(ifp)
  155.      struct ifnet *ifp;
  156. {
  157. #ifndef AMITCP
  158.     unsigned socksize, ifasize;
  159.     int namelen, unitlen;
  160.     char workbuf[12], *unitname;
  161.     register struct sockaddr_dl *sdl;
  162.     register struct ifaddr *ifa;
  163.     static int if_indexlim = 8;
  164. #endif
  165.     register struct ifnet **p = &ifnet;
  166.  
  167.     while (*p)
  168.         p = &((*p)->if_next);
  169.     *p = ifp;
  170.     ifp->if_index = ++if_index;
  171. #ifndef AMITCP
  172.     if (ifnet_addrs == 0 || if_index >= if_indexlim) {
  173.         unsigned n = (if_indexlim <<= 1) * sizeof(ifa);
  174.         struct ifaddr **q = (struct ifaddr **)
  175.                     bsd_malloc(n, M_IFADDR, M_WAITOK);
  176.         if (ifnet_addrs) {
  177.             aligned_bcopy((caddr_t)ifnet_addrs, (caddr_t)q, n/2);
  178.             bsd_free((caddr_t)ifnet_addrs, M_IFADDR);
  179.         }
  180.         ifnet_addrs = q;
  181.     }
  182.     /*
  183.      * create a Link Level name for this device
  184.      */
  185. #ifdef notanymore
  186.     /* Exec device name can contain digits, workaround with slash */
  187.     unitname = sprint_d((u_int)ifp->if_unit, 
  188.                 workbuf + 1, 
  189.                 sizeof(workbuf) - 1);
  190.     *--unitname = '/';
  191. #else
  192.     unitname = sprint_d((u_int)ifp->if_unit, workbuf, sizeof(workbuf));
  193. #endif
  194.     namelen = strlen(ifp->if_name);
  195.         unitlen = strlen(unitname);
  196. #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
  197.     socksize = _offsetof(struct sockaddr_dl, sdl_data[0]) +
  198.                    unitlen + namelen + ifp->if_addrlen;
  199. #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
  200.     socksize = ROUNDUP(socksize);
  201.     if (socksize < sizeof(*sdl))
  202.         socksize = sizeof(*sdl);
  203.     ifasize = sizeof(*ifa) + 2 * socksize;
  204.     ifa = (struct ifaddr *)bsd_malloc(ifasize, M_IFADDR, M_WAITOK);
  205.     if (ifa == 0)
  206.         return;
  207.     ifnet_addrs[if_index - 1] = ifa;
  208.     aligned_bzero((caddr_t)ifa, ifasize);
  209.     sdl = (struct sockaddr_dl *)(ifa + 1);
  210.     ifa->ifa_addr = (struct sockaddr *)sdl;
  211.     ifa->ifa_ifp = ifp;
  212.     sdl->sdl_len = socksize;
  213.     sdl->sdl_family = AF_LINK;
  214.     bcopy(ifp->if_name, sdl->sdl_data, namelen);
  215.     bcopy(unitname, namelen + (caddr_t)sdl->sdl_data, unitlen);
  216.     sdl->sdl_nlen = (namelen += unitlen);
  217.     sdl->sdl_index = ifp->if_index;
  218.     sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
  219.     ifa->ifa_netmask = (struct sockaddr *)sdl;
  220.     sdl->sdl_len = socksize - ifp->if_addrlen;
  221.     while (namelen != 0)
  222.         sdl->sdl_data[--namelen] = (char)0xff;
  223.     ifa->ifa_next = ifp->if_addrlist;
  224.     ifa->ifa_rtrequest = link_rtrequest;
  225.     ifp->if_addrlist = ifa;
  226. #endif
  227. }
  228.  
  229. /*
  230.  * Locate an interface based on a complete address.
  231.  */
  232.  
  233. /*
  234.  * Find an interface address suitable for host id. If no suitable interface
  235.  * address is found, the *id is not touched.
  236.  *
  237.  * This routine stops after the first non-loopback AF_INET address is found.
  238.  *
  239.  * This routine is specially made for the gethostid() system call.
  240.  */
  241. void
  242. findid(unsigned long *id)
  243. {
  244.   struct ifnet *ifp;
  245.   struct ifaddr *ifa;
  246.  
  247.   spl_t s = splimp();
  248.  
  249.   for (ifp = ifnet; ifp; ifp = ifp->if_next)
  250.     if (ifp->if_flags & IFF_UP)
  251.       for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
  252.     if (ifa->ifa_addr->sa_family == AF_INET) {
  253.       *id = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr;
  254.       if (!(ifp->if_flags & IFF_LOOPBACK))
  255.         goto ret;
  256.     }
  257.  ret:
  258.   splx(s);
  259. }
  260.  
  261. /*ARGSUSED*/
  262. struct ifaddr *
  263. ifa_ifwithaddr(addr)
  264.     register struct sockaddr *addr;
  265. {
  266.     register struct ifnet *ifp;
  267.     register struct ifaddr *ifa;
  268.  
  269. #define    equal(a1, a2) \
  270.   (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
  271.     for (ifp = ifnet; ifp; ifp = ifp->if_next)
  272.         for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) {
  273.         if (ifa->ifa_addr->sa_family != addr->sa_family)
  274.             continue;
  275.         if (equal(addr, ifa->ifa_addr))
  276.             return (ifa);
  277.         if ((ifp->if_flags & IFF_BROADCAST) && ifa->ifa_broadaddr &&
  278.             equal(ifa->ifa_broadaddr, addr))
  279.             return (ifa);
  280.     }
  281.     return ((struct ifaddr *)0);
  282. }
  283. /*
  284.  * Locate the point to point interface with a given destination address.
  285.  */
  286. /*ARGSUSED*/
  287. struct ifaddr *
  288. ifa_ifwithdstaddr(addr)
  289.     register struct sockaddr *addr;
  290. {
  291.     register struct ifnet *ifp;
  292.     register struct ifaddr *ifa;
  293.  
  294.     for (ifp = ifnet; ifp; ifp = ifp->if_next) 
  295.         if (ifp->if_flags & IFF_POINTOPOINT)
  296.         for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) {
  297.             if (ifa->ifa_addr->sa_family != addr->sa_family)
  298.                 continue;
  299.             if (equal(addr, ifa->ifa_dstaddr))
  300.                 return (ifa);
  301.     }
  302.     return ((struct ifaddr *)0);
  303. }
  304.  
  305. /*
  306.  * Find an interface on a specific network.  If many, choice
  307.  * is first found.
  308.  */
  309. struct ifaddr *
  310. ifa_ifwithnet(addr)
  311.     struct sockaddr *addr;
  312. {
  313.     register struct ifnet *ifp;
  314.     register struct ifaddr *ifa;
  315.     u_int af = addr->sa_family;
  316.  
  317.     if (af >= AF_MAX)
  318.         return (0);
  319. #ifndef AMITCP
  320.     if (af == AF_LINK) {
  321.         register struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
  322.         if (sdl->sdl_index && sdl->sdl_index <= if_index)
  323.         return (ifnet_addrs[sdl->sdl_index - 1]);
  324.     }
  325. #endif
  326.     for (ifp = ifnet; ifp; ifp = ifp->if_next)
  327.         for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) {
  328.         register char *cp, *cp2, *cp3;
  329.         register char *cplim;
  330.         if (ifa->ifa_addr->sa_family != af || ifa->ifa_netmask == 0)
  331.             continue;
  332.         cp = addr->sa_data;
  333.         cp2 = ifa->ifa_addr->sa_data;
  334.         cp3 = ifa->ifa_netmask->sa_data;
  335.         cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
  336.         for (; cp3 < cplim; cp3++)
  337.             if ((*cp++ ^ *cp2++) & *cp3)
  338.                 break;
  339.         if (cp3 == cplim)
  340.             return (ifa);
  341.         }
  342.     return ((struct ifaddr *)0);
  343. }
  344.  
  345. /*
  346.  * Find an interface using a specific address family
  347.  */
  348. struct ifaddr *
  349. ifa_ifwithaf(af)
  350.     register int af;
  351. {
  352.     register struct ifnet *ifp;
  353.     register struct ifaddr *ifa;
  354.  
  355.     for (ifp = ifnet; ifp; ifp = ifp->if_next)
  356.         for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
  357.         if (ifa->ifa_addr->sa_family == af)
  358.             return (ifa);
  359.     return ((struct ifaddr *)0);
  360. }
  361.  
  362. /*
  363.  * Find an interface address specific to an interface best matching
  364.  * a given address.
  365.  */
  366. struct ifaddr *
  367. ifaof_ifpforaddr(addr, ifp)
  368.     struct sockaddr *addr;
  369.     register struct ifnet *ifp;
  370. {
  371.     register struct ifaddr *ifa;
  372.     register char *cp, *cp2, *cp3;
  373.     register char *cplim;
  374.     struct ifaddr *ifa_maybe = 0;
  375.     u_int af = addr->sa_family;
  376.  
  377.     if (af >= AF_MAX)
  378.         return (0);
  379.     for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) {
  380.         if (ifa->ifa_addr->sa_family != af)
  381.             continue;
  382.         ifa_maybe = ifa;
  383.         if (ifa->ifa_netmask == 0) {
  384.             if (equal(addr, ifa->ifa_addr) ||
  385.                 (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
  386.                 return (ifa);
  387.             continue;
  388.         }
  389.         cp = addr->sa_data;
  390.         cp2 = ifa->ifa_addr->sa_data;
  391.         cp3 = ifa->ifa_netmask->sa_data;
  392.         cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
  393.         for (; cp3 < cplim; cp3++)
  394.             if ((*cp++ ^ *cp2++) & *cp3)
  395.                 break;
  396.         if (cp3 == cplim)
  397.             return (ifa);
  398.     }
  399.     return (ifa_maybe);
  400. }
  401.  
  402. #include <net/route.h>
  403.  
  404. /*
  405.  * Default action when installing a route with a Link Level gateway.
  406.  * Lookup an appropriate real ifa to point to.
  407.  * This should be moved to /sys/net/link.c eventually.
  408.  */
  409. void
  410. link_rtrequest(int cmd, register struct rtentry *rt, struct sockaddr *sa)
  411. {
  412.     register struct ifaddr *ifa;
  413.     struct sockaddr *dst;
  414.     struct ifnet *ifp;
  415.  
  416.     if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
  417.         ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
  418.         return;
  419.     if (ifa = ifaof_ifpforaddr(dst, ifp)) {
  420.         rt->rt_ifa = ifa;
  421.         if (ifa->ifa_rtrequest && 
  422.             ifa->ifa_rtrequest != link_rtrequest)
  423.             ifa->ifa_rtrequest(cmd, rt, sa);
  424.     }
  425. }
  426.  
  427. /*
  428.  * Mark an interface down and notify protocols of
  429.  * the transition.
  430.  * NOTE: must be called at splnet or eqivalent.
  431.  */
  432. void
  433. if_down(ifp)
  434.     register struct ifnet *ifp;
  435. {
  436.     register struct ifaddr *ifa;
  437.  
  438.     ifp->if_flags &= ~IFF_UP;
  439.     for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
  440.         pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
  441.     if_qflush(&ifp->if_snd);
  442. }
  443.  
  444. /*
  445.  * Flush an interface queue.
  446.  */
  447. void
  448. if_qflush(ifq)
  449.     register struct ifqueue *ifq;
  450. {
  451.     register struct mbuf *m, *n;
  452.  
  453.     n = ifq->ifq_head;
  454.     while (m = n) {
  455.         n = m->m_act;
  456.         m_freem(m);
  457.     }
  458.     ifq->ifq_head = 0;
  459.     ifq->ifq_tail = 0;
  460.     ifq->ifq_len = 0;
  461. }
  462.  
  463. /*
  464.  * Handle interface watchdog timer routines.  Called
  465.  * from softclock, we decrement timers (if set) and
  466.  * call the appropriate interface routine on expiration.
  467.  */
  468. void
  469. if_slowtimo()
  470. {
  471.   /*
  472.    * This routine is disabled since there are 
  473.    * no timeouts in our network interfaces
  474.    */
  475. #ifndef AMITCP            
  476.     register struct ifnet *ifp;
  477.     spl_t s = splimp();
  478.  
  479.     for (ifp = ifnet; ifp; ifp = ifp->if_next) {
  480.         if (ifp->if_timer == 0 || --ifp->if_timer)
  481.             continue;
  482.         if (ifp->if_watchdog)
  483.             (*ifp->if_watchdog)(ifp->if_unit);
  484.     }
  485.     splx(s);
  486. #ifndef AMITCP
  487.     /*
  488.      * Timeouts are scheduled from amiga_time.c in AmiTCP/IP.
  489.      */
  490.     timeout(if_slowtimo, (caddr_t)0, hz / IFNET_SLOWHZ);
  491. #endif
  492. #endif
  493. }
  494.  
  495. /*
  496.  * Map interface name to
  497.  * interface structure pointer.
  498.  */
  499. struct ifnet *
  500. ifunit(name)
  501.     register char *name;
  502. {
  503.     register struct ifnet *ifp;
  504.     register char *cp;
  505.     int unit;
  506.     unsigned len;
  507.     char *ep, c;
  508.  
  509.     for (cp = name; cp < name + IFNAMSIZ && *cp; cp++)
  510.         if (*cp >= '0' && *cp <= '9')
  511.             break;
  512.     if (*cp == '\0' || cp == name + IFNAMSIZ)
  513.         return ((struct ifnet *)0);
  514.     /*
  515.      * Save first char of unit, and pointer to it,
  516.      * so we can put a null there to avoid matching
  517.      * initial substrings of interface names.
  518.      */
  519.     len = cp - name + 1;
  520.     c = *cp;
  521.     ep = cp;
  522.     for (unit = 0; *cp >= '0' && *cp <= '9'; )
  523.         unit = unit * 10 + *cp++ - '0';
  524.     *ep = 0;
  525.  
  526.     for (ifp = ifnet; ifp; ifp = ifp->if_next) {
  527.         if (bcmp(ifp->if_name, name, len))
  528.             continue;
  529.         if (unit == ifp->if_unit)
  530.             break;
  531.     }
  532.     *ep = c;
  533.     {
  534.         extern struct ifnet *iface_find(char *, short unit);
  535.         if (ifp == 0)
  536.         ifp = iface_find(name, unit);
  537.     }
  538.     return (ifp);
  539. }
  540.  
  541. /*
  542.  * Interface ioctls.
  543.  */
  544. int
  545. ifioctl(so, cmd, data)
  546.     struct socket *so;
  547.     int cmd;
  548.     caddr_t data;
  549. {
  550.     register struct ifnet *ifp;
  551.     register struct ifreq *ifr;
  552. #ifndef AMITCP
  553.     int error;
  554. #endif
  555.     extern int arpioctl(int cmd, caddr_t data);
  556.  
  557.     switch (cmd) {
  558.  
  559.     case SIOCGIFCONF:
  560.         return (ifconf(cmd, data));
  561.  
  562. #ifdef COMPAT_AMITCP2
  563.     case ASIOCGIFCONF:
  564.         return (aifconf(cmd, data));
  565. #endif
  566.  
  567. #if INET && NETHER > 0
  568.     case SIOCSARP:
  569.     case SIOCDARP:
  570. #ifndef AMITCP /* no protection on AmigaOS */
  571.         if (error = suser(p->p_ucred, &p->p_acflag))
  572.             return (error);
  573.         /* FALL THROUGH */
  574. #else
  575.     case SIOCGARPT:
  576. #endif /* AMITCP */
  577.     case SIOCGARP:
  578.         return (arpioctl(cmd, data));
  579. #endif
  580.     }
  581.  
  582. #ifndef COMPAT_AMITCP2
  583.     /* Do we have old ioctl? */
  584.     if (IOCGROUP(cmd) == 'I') {
  585.         struct aifreq *aifr;
  586.         aifr = (struct aifreq *)data;
  587.         /* Nobody needs interface name after we have got ifp */
  588.         ifp = aifunit(aifr->ifr_name);
  589.         ifr = (struct ifreq *)(data + AIFNAMSIZ - IFNAMSIZ);
  590.         cmd -= ASIOCSIFADDR - SIOCSIFADDR;
  591.     } 
  592.     else 
  593. #endif
  594.     {
  595.         ifr = (struct ifreq *)data;
  596.         ifp = ifunit(ifr->ifr_name);
  597.     }
  598.  
  599.     if (ifp == 0)
  600.         return (ENXIO);
  601.     switch (cmd) {
  602.  
  603.     case SIOCGIFFLAGS:
  604.         ifr->ifr_flags = ifp->if_flags;
  605.         break;
  606.  
  607.     case SIOCGIFMETRIC:
  608.         ifr->ifr_metric = ifp->if_metric;
  609.         break;
  610.  
  611.     case SIOCSIFFLAGS:
  612. #ifndef AMITCP /* no protection on AmigaOS */
  613.         if (error = suser(p->p_ucred, &p->p_acflag))
  614.             return (error);
  615. #endif /* AMITCP */
  616.         /* if_down() is kludged for Sana-II driver ioctl */
  617.         if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
  618.             spl_t s = splimp();
  619.             if_down(ifp);
  620.             splx(s);
  621.         }
  622.         ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
  623.             (ifr->ifr_flags &~ IFF_CANTCHANGE);
  624.         if (ifp->if_ioctl)
  625.             (void) (*ifp->if_ioctl)(ifp, cmd, data);
  626.         break;
  627.  
  628.     case SIOCSIFMETRIC:
  629. #ifndef AMITCP /* no protection on AmigaOS */
  630.         if (error = suser(p->p_ucred, &p->p_acflag))
  631.             return (error);
  632. #endif /* AMITCP */
  633.         ifp->if_metric = ifr->ifr_metric;
  634.         break;
  635.  
  636.     default:
  637.         if (so->so_proto == 0)
  638.             return (EOPNOTSUPP);
  639.         return ((*so->so_proto->pr_usrreq)(so,
  640.                            PRU_CONTROL,
  641.                            (struct mbuf *)cmd,
  642.                            (struct mbuf *)ifr,
  643.                            (struct mbuf *)ifp));
  644.     }
  645.     return (0);
  646. }
  647.  
  648. /*
  649.  * Return interface configuration
  650.  * of system.  List may be used
  651.  * in later ioctl's (above) to get
  652.  * other information.
  653.  */
  654. int
  655. ifconf(cmd, data)
  656.     int cmd;
  657.     caddr_t data;
  658. {
  659.     register struct ifconf *ifc = (struct ifconf *)data;
  660.     register struct ifnet *ifp = ifnet;
  661.     register struct ifaddr *ifa;
  662.     register char *cp, *ep;
  663.     struct ifreq ifr, *ifrp;
  664.     int space = ifc->ifc_len, error = 0;
  665.  
  666.     ifrp = ifc->ifc_req;
  667. #ifndef AMITCP
  668.     ep = ifr.ifr_name + sizeof (ifr.ifr_name) - 2;
  669. #endif
  670.     for (; space > sizeof (ifr) && ifp; ifp = ifp->if_next) {
  671. #ifdef AMITCP
  672.         ep = sprint_d(ifp->if_unit, ifr.ifr_name, sizeof(ifr.ifr_name));
  673.         /* Copy the interface name into ifr */
  674.         bcopy(ifp->if_name, ifr.ifr_name, ep - ifr.ifr_name);
  675.         /* Find the end of interface name */
  676.         for (cp = ifr.ifr_name; cp < ep && *cp; cp++)
  677.             ;
  678.         /* Append unit number to it */
  679.         for (; *cp = *ep; cp++, ep++)
  680.             ;
  681. #else
  682.         bcopy(ifp->if_name, ifr.ifr_name, sizeof (ifr.ifr_name) - 2);
  683.         for (cp = ifr.ifr_name; cp < ep && *cp; cp++)
  684.             ;
  685.         *cp++ = '0' + ifp->if_unit; *cp = '\0';
  686. #endif
  687.         if ((ifa = ifp->if_addrlist) == 0) {
  688.             aligned_bzero_const((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
  689. #ifdef AMITCP
  690.             *ifrp = ifr;
  691. #else
  692.             error = copyout((caddr_t)&ifr, (caddr_t)ifrp, sizeof (ifr));
  693.             if (error)
  694.                 break;
  695. #endif
  696.             space -= sizeof (ifr), ifrp++;
  697.         } else 
  698.             for ( ; space > sizeof (ifr) && ifa; ifa = ifa->ifa_next) {
  699.             register struct sockaddr *sa = ifa->ifa_addr;
  700. #ifdef COMPAT_43
  701.             if (cmd == OSIOCGIFCONF) {
  702.                 struct osockaddr *osa =
  703.                      (struct osockaddr *)&ifr.ifr_addr;
  704.                 ifr.ifr_addr = *sa;
  705.                 osa->sa_family = sa->sa_family;
  706.                 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
  707.                         sizeof (ifr));
  708.                 ifrp++;
  709.             } else
  710. #endif
  711.             if (sa->sa_len <= sizeof(*sa)) {
  712.                 ifr.ifr_addr = *sa;
  713. #ifdef AMITCP
  714.                 *ifrp = ifr;
  715. #else
  716.                 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
  717.                         sizeof (ifr));
  718. #endif
  719.                 ifrp++;
  720.             } else {
  721.                 space -= sa->sa_len - sizeof(*sa);
  722.                 if (space < sizeof (ifr))
  723.                     break;
  724. #ifdef AMITCP
  725.                 aligned_bcopy_const((caddr_t)&ifr, 
  726.                             (caddr_t)ifrp,
  727.                             sizeof (ifr.ifr_name));
  728.                 aligned_bcopy((caddr_t)sa,
  729.                       (caddr_t)&ifrp->ifr_addr, sa->sa_len);
  730. #else
  731.                 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
  732.                         sizeof (ifr.ifr_name));
  733.                 if (error == 0)
  734.                     error = copyout((caddr_t)sa,
  735.                       (caddr_t)&ifrp->ifr_addr, sa->sa_len);
  736. #endif
  737.                 ifrp = (struct ifreq *)
  738.                     (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
  739.             }
  740. #ifndef AMITCP
  741.             if (error)
  742.                 break;
  743. #endif
  744.             space -= sizeof (ifr);
  745.         }
  746.     }
  747.     ifc->ifc_len -= space;
  748.     return (error);
  749. }
  750.  
  751. int aifconf(int cmd, caddr_t data)
  752. {
  753.     return ENOSYS;
  754. }
  755.  
  756. static char *
  757. sprint_d(n, buf, buflen)
  758.     u_int n;
  759.     char *buf;
  760.     int buflen;
  761. {
  762.     register char *cp = buf + buflen - 1;
  763.  
  764.     *cp = 0;
  765.     do {
  766.         cp--;
  767.         *cp = "0123456789"[n % 10];
  768.         n /= 10;
  769.     } while (n != 0);
  770.     return (cp);
  771. }
  772.